package com.resource.studentmanage.service.impl;


import java.util.Map;
import java.util.Objects;
import java.util.Set;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.resource.common.utils.PageUtils;
import com.resource.common.utils.Query;
import com.resource.studentmanage.entity.TStudentExamEntity;
import com.resource.studentmanage.mapper.TStudentExamDao;
import com.resource.studentmanage.service.TStudentExamService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;


@Service
@Slf4j
public class TStudentExamServiceImpl extends ServiceImpl<TStudentExamDao, TStudentExamEntity> implements TStudentExamService {

    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        int size = params.size();
        IPage<TStudentExamEntity> page=new Page<>();
        if(size==4){
            String keys="";
            String param="";
            Set<String> strings = params.keySet();
            for (String key : strings) {
                keys=key;
                param=params.get(keys).toString();
            }
            page = this.page(
                    new Query<TStudentExamEntity>().getPage(params),
                    new QueryWrapper<TStudentExamEntity>().eq(keys,param)
            );
            return new PageUtils(page);
        }
         page = this.page(
                new Query<TStudentExamEntity>().getPage(params),
                new QueryWrapper<TStudentExamEntity>()
        );

        return new PageUtils(page);
    }

    @Override
    public void submitEaxm(Map<String, String> map) {
        TStudentExamEntity tStudentExamEntity = new TStudentExamEntity();
        tStudentExamEntity.setExamCode(map.get("examCode"));
        tStudentExamEntity.setExamName(map.get("examName"));
        tStudentExamEntity.setTeacherName(map.get("examTeacherName"));
        tStudentExamEntity.setStudentCode(map.get("examStudentCode"));
        tStudentExamEntity.setStudentName(map.get("examStudentName"));
        tStudentExamEntity.setExamProgress(map.get("examProgress"));
        this.saveOrUpdate(tStudentExamEntity);
    }

    @Override
    public void submitMessageByExam(Map<String, String> map) {
        String studentCode = map.get("studentCode");
        String messageContent = map.get("messageContent");
        String examGrade = map.get("examGrade");
        String teacherName = map.get("teacherName");
        log.error("开始更新考试状态");
        log.error("学生编号:"+studentCode);
        log.error("教师姓名:"+teacherName);
        TStudentExamEntity studentExamSelect = this.getOne(new QueryWrapper<TStudentExamEntity>().eq("student_code", studentCode).eq("teacher_name",teacherName));
        if(!Objects.isNull(studentExamSelect)){
            studentExamSelect.setExamGrades(examGrade);
            studentExamSelect.setExamProgress("已完成");
            this.updateById(studentExamSelect);
            log.error("更新学生考试信息成功");
        }else{
            log.error("未获取到指定学号的学生考试信息");
            return;
        }
    }
}